/*
 * @Author: TC (tengchao@taqu.cn)
 * @Date: 2025-10-20 16:04:49
 * @Last Modified by: TC (tengchao@taqu.cn)
 * @Last Modified time: 2025-10-21 15:08:39
 */

import { _decorator, Node } from "cc";
import { SuperLayout } from "db://assets/FrameWork/CCComponent/SuperScrollView/SuperLayout";
import { BaseUI } from "db://assets/FrameWork/Module/Component/BaseUI";
import { nsMoleShopCfg } from "../../Config/MoleShopCfg";
import { app } from "db://assets/FrameWork/App";
import { MoleShopItem } from "./MoleShopItem";
const { ccclass, property } = _decorator;

/**
 * 商城界面
 */
@ccclass("MoleShopUI")
export class MoleShopUI extends BaseUI {
    @property({ type: Node, displayName: "关闭按钮" })
    private btnClose: Node = null;
    @property({ type: SuperLayout, displayName: "商品列表" })
    layoutShop: SuperLayout = null;

    private _cfgs: IMoleCfgInterface.IShop[] = null;

    protected init(): void {
        super.init();
    }

    protected addUIEvent(): void {
        super.addUIEvent();
        this.addClickEvent(this.btnClose, this._onClose);
    }

    protected onShow(): void {
        super.onShow();
        this._cfgs = nsMoleShopCfg.getAllShopCfgs();
        if (!this._cfgs || this._cfgs.length <= 0) {
            app.log.error(`未找到商城配置`);
            return;
        }
        this.layoutShop.total(this._cfgs.length);
        this.layoutShop.scrollToHeader(0);
    }

    /**
     * 刷新虚拟列表item
     * @param item
     * @param index
     */
    onRefreshShopItem(item: Node, index: number) {
        item.getComponent(MoleShopItem).refreshUI(this._cfgs[index]);
    }
    /**
     * 点击关闭
     */
    private _onClose(): void {
        this.closeUI();
    }
}
